const express = require('express');
const multer = require('multer');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 处理上传路由
app.post('/upload', multer().none(), (req, res) => {
  try {
    // 从 FormData 中解析上传数据
    const uploadFiles = JSON.parse(req.body.uploadFiles);
    
    console.log('Received uploadFiles:', uploadFiles);

    // 模拟保存操作
    uploadFiles.forEach((fileBuffer, index) => {
      console.log(`File ${index + 1} size:`, fileBuffer.length);
    });

    res.status(200).send({ message: 'Files uploaded successfully!' });
  } catch (error) {
    console.error('Error processing upload:', error);
    res.status(500).send({ message: 'Error processing upload' });
  }
});

// 启动服务器
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}`);
});
